<?php

//require CAMINHO_SERVIDOR.'conexao.php';
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of geralDAO
 *
 * @author Fernando
 */
class geralDAO {

    private $con;

    function __construct() {
        self::setCon(conectar());
        mysql_query('SET CHARACTER SET utf8');
    }

    public function getCon() {
        return $this->con;
    }

    public function setCon($con) {
        $this->con = $con;
    }

    public function fecharConexao() {
        desconectar(self::getCon());
    }

    function executaQuery($query) {
        return mysql_query($query, self::getCon());
    }

    function executaQueryAll($query) {
        $posts = array();
        $result = mysql_query($query, self::getCon());

        if (mysql_num_rows($result)) {
            while ($post = mysql_fetch_assoc($result)) {
                $posts[] = $post;
            }
        }

        return $posts;
    }

    function executaQueryRow($query) {
        $result = mysql_query($query, self::getCon());
        return mysql_fetch_assoc($result);
    }

    function deletaRegistroById($tabela, $chave, $id_registro) {
        $query = "DELETE FROM " . $tabela . " WHERE " . $chave . "=" . $id_registro;
        if (self::executaQuery($query)) {
            $result = true;
        } else {
            $result = false;
        }
        return $result;
    }

    function getStatusById($tabela, $chave, $id_registro) {
        $query = "SELECT status FROM " . $tabela . " WHERE " . $chave . "=" . $id_registro;
        $aux = self::executaQueryRow($query);
        $result = $aux['status'];
        return $result;
    }

    function alteraStatusRegistroById($tabela, $chave, $id_registro) {
        $status = ($this->getStatusById($tabela, $chave, $id_registro) == 'N') ? 'S' : 'N';
        $query = "UPDATE " . $tabela . " SET status='" . $status . "' WHERE " . $chave . "=" . $id_registro;
        if (self::executaQuery($query)) {
            $result = true;
        } else {
            $result = false;
        }
        return $result;
    }

}
